<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;
    use HasFactory;

    public function topics(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Topic::class);
    }

    public function updateTopicCount()
    {
        $this->topic_count = $this->topics->count();
        $this->save();
    }
}
